const express = require('express')
const userModel = require('../db/model/userMoodel')


const app = express()

app.get('/login',(req,res)=>{
    res.render('login.html');
})

app.post('/login',(req,res)=>{
    let username = req.body.username;
    let pwd = req.body.pwd;

    userModel.findOne({username,pwd},(err,data)=>{
        if(data){
            req.session.username = username;

            res.render('jump.html',{name:"登录成功了"})
        }else{
            res.send("输入有误，请重新输入")
        }
    })
})

app.get('/register',(req,res)=>{
    res.render('register.html')
})

app.post('/register',(req,res)=>{
    let username = req.body.username;
    let pwd = req.body.pwd

    userModel.find({username},(err,data)=>{
        if(data.length){
            res.send('该用户名已被占用，请重新换一个吧')
        }else{
            userModel.create({username,pwd},(err,data)=>{
                res.render('jump.html',{name:"恭喜，注册成功了"})
            })
        }
    })
})

app.get('/logout',(req,res)=>{
    req.session.destroy(()=>{
        res.render('jump.html',{name:"已成功退出"})
    })
})

module.exports = app